package com.cn.service.impl;

import com.cn.mapper.IdleItemMapper;
import com.cn.mapper.OrderMapper;
import com.cn.mapper.UserMapper;
import com.cn.entity.IdleItem;
import com.cn.entity.Order;
import com.cn.entity.User;
import com.cn.service.OrderService;
import com.cn.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private IdleItemMapper idleItemMapper;
    
    @Autowired
    private UserMapper userMapper;

    /**
     * 获取订单信息，同时获取对应的闲置信息
     * @param id
     * @return
     */
    public Order getOrder(Long id){
        Order order = orderMapper.selectByPrimaryKey(id);
        order.setIdleItem(idleItemMapper.selectByPrimaryKey(order.getIdleId()));
        return order;
    }

    /**
     * 根据订单号，查询订单
     *
     * @return*/
    @Override
    public PageResult<Order> findOrderByNumber(String searchValue, int page, int nums) {
        // 使用PageHelper进行分页
        PageHelper.startPage(page, nums);
        List<Order> list = orderMapper.getOrderByNumberForPage(searchValue);
        
        // 返回PageResult对象
        return new PageResult<>(new PageInfo<>(list).getTotal(), list);
    }

    public PageResult<Order> getAllOrder(int page, int nums){
        // 使用PageHelper进行分页
        PageHelper.startPage(page, nums);
        List<Order> list = orderMapper.getAllOrderForPage();
        
        // 返回PageResult对象
        return new PageResult<>(new PageInfo<>(list).getTotal(), list);
    }

    public boolean deleteOrder(long id){
        return orderMapper.deleteByPrimaryKey(id) == 1;
    }
}
